
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:jd_shopping/services/ScreenAdaper.dart';
import 'package:jd_shopping/Model/ProductListModel.dart';
import 'package:jd_shopping/RequestMethod/httpRequestMtehod.dart';
import 'package:jd_shopping/HttpRequest/configDomain.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';



class SearchProductList extends StatefulWidget {
  final Map arguments;
  SearchProductList({Key key, this.arguments});
  @override
  _SearchProductListState createState() => _SearchProductListState(arguments: arguments);
}

class _SearchProductListState extends State<SearchProductList> {

  final Map arguments;
  _SearchProductListState({Key key, this.arguments});
  final GlobalKey<ScaffoldState> _scaffoldKey =  GlobalKey<ScaffoldState>();
  List _productModelList = [];
  int _currentIndex = 0;
  bool _salecountCurrentIndex = false;
  bool _priceCurrentIndex = false;
  ScrollController _scrollController = ScrollController();
  RefreshController _refreshController =
      RefreshController(initialRefresh: false);
  int page=1;
  String _keywords;
 

  @override
  void initState() { 
     String cid =  '${arguments['cid']}';
     _keywords =  '${arguments['keywords']}';
     _getProductListWithCidAndSort(cid,'',page,_keywords);
    super.initState();
  }
  _getProductListWithCidAndSort(String queryParametersCid,String queryParametersSort,int page,String search){
     HttpRequestMethod.getProductListData(queryParametersCid,queryParametersSort,page,search).then((response){
         var focusList = ProductListModel.fromJson(response.data);
          setState(() {
           if (focusList.result.length != 0) {
             this._productModelList.addAll(focusList.result);
           }
           print('this._productModelList----->${ this._productModelList}');
        });
     });
  }
  Widget _topSelectBar() {
    return Positioned(
      top: 0,
      height: ScreenAdaper.height(80),
      width: ScreenAdaper.getScreenWidth(),
      child: Container(
        height: ScreenAdaper.height(80),
        width: ScreenAdaper.getScreenWidth(),
         decoration: BoxDecoration(
            border: Border(
                bottom: BorderSide(
                    width: 1, color: Color.fromRGBO(233, 233, 233, 0.9)))),
        child: Row(
          children: <Widget>[
            Expanded(
              flex: 1,
              child: InkWell(
                child: Padding(
                  padding: EdgeInsets.fromLTRB(
                      0, ScreenAdaper.height(16), 0, ScreenAdaper.height(16)),
                  child: Text(
                    "综合",
                    textAlign: TextAlign.center,
                    style: TextStyle(color: _currentIndex==0?Colors.red:Colors.grey),
                  ),
                ),
                onTap: () {
                  setState(() {
                    _currentIndex = 0;
                     String cid =  '${arguments['cid']}';
                      _keywords =  '${arguments['keywords']}';
                    _getProductListWithCidAndSort(cid, '',page,_keywords);
                  });
                },
              ),
            ),
            Expanded(
              flex: 1,
              child: InkWell(
                child: Padding(
                  padding: EdgeInsets.fromLTRB(
                      0, ScreenAdaper.height(16), 0, ScreenAdaper.height(16)),
                  child: Row(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: <Widget>[
                        Text("销量",
                            textAlign: TextAlign.center,
                             style: TextStyle(color: _currentIndex==1?Colors.red:Colors.grey),
                            ),
                       _salecountCurrentIndex ?Icon(Icons.arrow_drop_up,color: _currentIndex==1?Colors.red:Colors.grey,):Icon(Icons.arrow_drop_down,color:_currentIndex==1?Colors.red:Colors.grey,)
                      ]
                  )
                ),
                onTap: () {
                   setState(() {
                    _currentIndex = 1;
                    _salecountCurrentIndex = !_salecountCurrentIndex;
                    _priceCurrentIndex = false;
                     String cid =  '${arguments['cid']}';
                     _keywords =  '${arguments['keywords']}';
                    _getProductListWithCidAndSort(cid, 'salecount_1',page,_keywords);
                    _scrollController.jumpTo(0);
                  });
                },
              ),
            ),
             Expanded(
              flex: 1,
              child: InkWell(
                child: Padding(
                  padding: EdgeInsets.fromLTRB(
                      0, ScreenAdaper.height(16), 0, ScreenAdaper.height(16)),
                  child: Row(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: <Widget>[
                        Text("价格",
                            textAlign: TextAlign.center,
                             style: TextStyle(color: _currentIndex==2?Colors.red:Colors.grey),
                             ),
                       _priceCurrentIndex ?Icon(Icons.arrow_drop_up,color: _currentIndex==2?Colors.red:Colors.grey,):Icon(Icons.arrow_drop_down,color:_currentIndex==2?Colors.red:Colors.grey,)
                      ]
                  )
                ),
                onTap: () {
                   setState(() {
                    _salecountCurrentIndex = false;
                    _priceCurrentIndex = !_priceCurrentIndex;
                    _currentIndex = 2;
                     String cid =  '${arguments['cid']}';
                      _keywords =  '${arguments['keywords']}';
                    _getProductListWithCidAndSort(cid, 'price_1',page,_keywords);
                     _scrollController.jumpTo(0);
                  });
                },
              ),
            ),
            Expanded(
              flex: 1,
              child: InkWell(
                child: Padding(
                  padding: EdgeInsets.fromLTRB(
                      0, ScreenAdaper.height(16), 0, ScreenAdaper.height(16)),
                  child: Text(
                  "筛选", 
                  textAlign: TextAlign.center,
                  style: TextStyle(color: _currentIndex==3?Colors.red:Colors.grey),
                  ),
                ),
                onTap: () {
                  setState(() {
                    _currentIndex = 3;
                     _scaffoldKey.currentState.openEndDrawer();
                  });
                },
              ),
            ),
          ],
        ),
      ),
    );
  }
  _onRefresh() async{
    print('_onRefresh');
     this._productModelList.clear();
     this.page++;
     String cid =  '${arguments['cid']}';
      _keywords =  '${arguments['keywords']}';
     _getProductListWithCidAndSort(cid, '', this.page,_keywords);
     _refreshController.refreshCompleted();
  }
  _onLoading() async{
    print('_onLoading');
    this.page++;
    String cid =  '${arguments['cid']}';
     _keywords =  '${arguments['keywords']}';
    _getProductListWithCidAndSort(cid, '',this.page,_keywords);
    _refreshController.loadComplete();
  }
  
  Widget _productlist() {
    return Container(
        padding: EdgeInsets.all(10),
        margin: EdgeInsets.only(top: ScreenAdaper.height(80)),
        child: SmartRefresher(
        enablePullDown: true,
        enablePullUp: true,
        header: WaterDropHeader(),
        footer:ClassicFooter(),
      controller: _refreshController,
      onRefresh: _onRefresh,
      onLoading: _onLoading,
      child: ListView.builder(
        controller: _scrollController,
        itemBuilder: (context, index) {
           String sPic= this._productModelList[index].sPic;
           sPic= ConfigDomian.domain + '/' + sPic.replaceAll('\\', '/');
          //每一个元素
          return Column(
            children: <Widget>[
              Row(
                children: <Widget>[
                  Container(
                    width: ScreenAdaper.width(180),
                    height: ScreenAdaper.height(180),
                    child: Image.network(
                        sPic,
                        fit: BoxFit.cover),
                  ),
                  Expanded(
                    flex: 1,
                    child: Container(
                      height: ScreenAdaper.height(180),
                      margin: EdgeInsets.only(left: 10),
                      // color: Colors.red,
                      child: Column(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: <Widget>[
                          Text(
                              this._productModelList[index].title,
                              maxLines: 2,
                              overflow: TextOverflow.ellipsis),
                          Row(
                            children: <Widget>[
                              Container(
                                height: ScreenAdaper.height(36),
                                margin: EdgeInsets.only(right: 10),
                                padding: EdgeInsets.fromLTRB(10, 0, 10, 0),
                                //注意 如果Container里面加上decoration属性，这个时候color属性必须得放在BoxDecoration
                                decoration: BoxDecoration(
                                  borderRadius: BorderRadius.circular(10),
                                  color: Color.fromRGBO(230, 230, 230, 0.9),
                                ),
                                child: Text("4g"),
                              ),
                              Container(
                                height: ScreenAdaper.height(36),
                                margin: EdgeInsets.only(right: 10),
                                padding: EdgeInsets.fromLTRB(10, 0, 10, 0),
                                decoration: BoxDecoration(
                                  borderRadius: BorderRadius.circular(10),
                                  color: Color.fromRGBO(230, 230, 230, 0.9),
                                ),
                                child: Text("126"),
                              ),
                            ],
                          ),
                          Text(
                            '¥${this._productModelList[index].price}',
                            style: TextStyle(color: Colors.red, fontSize: 16),
                          )
                        ],
                      ),
                    ),
                  )
                ],
              ),
              Divider(height: 20)
            ],
          );
        },
        itemCount:this._productModelList.length,
      ), 
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    ScreenAdaper.init(context);
    return Scaffold(
        key: _scaffoldKey,
        appBar: AppBar(
          title: Text('${arguments['title']}'),
          actions: <Widget>[
            Text("")
          ],
          ),
        endDrawer: Drawer(
          child: Container(
            child: Text("实现筛选功能"),
          ),
        ),
        body: Stack(
          children: <Widget>[
           _topSelectBar(),
           _productlist()
           ],
        ));
  }
}
